package com.ccgk.logistics.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ccgk.common.MyCollectionUtils;
import com.ccgk.common.page.PaginationVo;
import com.ccgk.common.web.RequestUtils;
import com.ccgk.logistics.domain.Company;
import com.ccgk.logistics.domain.Container;
import com.ccgk.logistics.service.ContainerService;

@Controller
@RequestMapping("/container")
public class ContainerController {
	
	@Autowired
	private ContainerService containerService;
	
	/**查询公司列表*/
	@RequestMapping("/list")
	public String list(HttpServletRequest request){
		int currentPage = RequestUtils.getInt(request,"currentPage",1);
		int pageSize = RequestUtils.getInt(request,"pageSize",PaginationVo.DEFAULT_PAGESIZE);
		
		ContainerService.ContainerCriteria  criteria = new ContainerService.ContainerCriteria();
		String size = request.getParameter("size");
		if(StringUtils.isNotBlank(size))
			criteria.sizeLike(size);
		PaginationVo<Container> result = containerService.findList(criteria, currentPage, pageSize);
		request.setAttribute("result",result);
		return "container/list";
	}
	
	/**获取单个公司*/
	@RequestMapping("/{id}")
	public String getById(@PathVariable("id")Integer id,Model model){
		Container container = containerService.getById(id);
		model.addAttribute("obj", container);
		return "container/edit";
	}
	
	/**更新公司信息*/
	@ResponseBody
	@RequestMapping(value="/{id}",method=RequestMethod.POST)
	public String update(Container container){
		Container _container = containerService.getBySize(container.getSize());
		if(_container != null && !_container.getId().equals(container.getId()))
			return "{flag:1}";
		containerService.update(container);
		return "{flag:0}";
	}
	/**新增公司页*/
	@RequestMapping("/add")
	public String showAdd(){
		return "container/add";
	}
	/**新增公司*/
	@ResponseBody
	@RequestMapping(value="/add",method=RequestMethod.POST)
	public String add(Container container,HttpServletResponse response){
		if(containerService.getBySize(container.getSize()) != null){
			return "{flag:1}";
		}
		containerService.add(container);
		return "{flag:0}";
	}
	/**删除公司*/
	@ResponseBody
	@RequestMapping("/delete")
	public String delete(@RequestParam("id")String ids,Model model){
		Integer[] idIntArray = MyCollectionUtils.splitToIntArray(ids);
		containerService.batchDelete(idIntArray);
		return "{success:true}";
	}
}
